package nautilus.placetaker;

import java.io.File;
import java.util.ArrayList;

import nautilus.placetaker.dao.AccountDAO;
import nautilus.placetaker.dto.Place;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;

public class ListPlaces extends Activity{
	
	PlaceListAdapter adapter;
	ArrayList<Place> places;
	ListView listPlaces;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_place);
		listPlaces = (ListView)findViewById(R.id.lvPlaces);
		
		AsyncTask<Void, Void, Void> loadTask = new AsyncTask<Void, Void, Void>(){

			@Override
			protected Void doInBackground(Void... arg0) {
				AccountDAO dao = new AccountDAO(ListPlaces.this);
				places = dao.getAllPlaces();
				return null;
			}
			
			@Override
			protected void onPostExecute(Void result){
				super.onPostExecute(result);
				if(adapter == null){
					adapter = new PlaceListAdapter(ListPlaces.this, places, (new File(MainActivity.PICTURE_STORE)).getAbsolutePath());
					listPlaces.setAdapter(adapter);
					return;
				}
				adapter.notifyDataSetChanged();
			}
			
		};
		loadTask.execute();
	}
}
